<?php /* УТФ-8:fix */

class SPImodel__adv__manage extends SPImodel__adv
{
    public function select( $filter_search = '', $orderby = null )
    {
        switch( $orderby['dir'] )
        {
            default:
            case '+':   $orderby['dir'] = 'ASC';  break;
            case '-':   $orderby['dir'] = 'DESC'; break;
        }

        switch( $orderby['field'] )
        {
            default:
            case 'id':              $q_orderby = "id {$orderby['dir']}"; break;
            case 'name':            $q_orderby = "name {$orderby['dir']}, id ASC"; break;
            case 'type':            $q_orderby = "type {$orderby['dir']}, id ASC"; break;
            case 'position':        $q_orderby = "position {$orderby['dir']}, id ASC"; break;
            case 'click_count':     $q_orderby = "click_count {$orderby['dir']}, id ASC"; break;
            case 'showed_count':    $q_orderby = "showed_count {$orderby['dir']}, id ASC"; break;
            case 'ctr':             $q_orderby = "ctr {$orderby['dir']}"; break;
        }

        $store = SPIstore::I();

        $query =    " SELECT * "
               .         " , CASE WHEN showed_count = 0 "
               .                " THEN 0 "
               .                " ELSE click_count::decimal / showed_count * 100 "
               .                " END AS ctr "
               .      " FROM ".SPIcfg::$store['table:adv']
               .     " WHERE TRUE "
               . ( $filter_search
                 ? '     AND (  ' . $store->prepare_search_query( 'id', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'name', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'link', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'code', $filter_search )
                 . '         ) '
                 : ''
                 )
               . " ORDER BY {$q_orderby} "
               ;

        return $store->query( $query );
    }

    public function get_by_id( $item_id )
    {
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:adv']
               .     " WHERE id=" . (int) $item_id
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function set_activity( $set_activity_value, $item_id )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:adv'],
            array( 'workstatus' => ($set_activity_value == 'on' ? 1 : 0 ) ),
            array( 'id' => $item_id )
        );
    }

    public function insert( $fields )
    {
        $fields['pid_profile_addby'] = SPI::I('Iam')->get_id();

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:adv'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_id, $fields )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:adv'],
            $fields,
            array( 'id' => $item_id )
        );
    }

    public function delete( $item_id )
    {
        return SPIstore::I()->delete(
            SPIcfg::$store['table:adv'],
            array( 'id' => $item_id )
        );
    }
}
